package tk.djcrazy.instantmessage.action;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.domain.UserEntity;
import tk.djcrazy.instantmessage.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class ChangePassword extends ActionSupport implements SessionAware{
	private static final long serialVersionUID = -8878292398637590534L;
	private String oldPassword;
	private String newPassword;
	private String confirmPassword;
	private String result = "failed";
	private String message;
	private Map<String, Object> session;
	@Resource
	private IUserService userService;

	public String execute() {
		try {
			UserEntity entity = userService.findById((Integer) session
					.get("uid"));
			if (oldPassword.equals(entity.getPassword())
					&& newPassword.equals(confirmPassword)) {
				entity.setPassword(newPassword);
				userService.save(entity);
				result = "success";
				setMessage("修改密码成功");
			} else {
				result = "failed";
				setMessage("修改密码失败");
			}
		} catch (Exception e) {
			result = "failed";
			setMessage("未知错误");
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}
